//
//  NewsViewController.swift
//  KaKaWallet
//
//  Created by admin on 16/4/22.
//  Copyright © 2016年 ibendi. All rights reserved.
//

import UIKit

class NewsViewController: SuperViewController,UITableViewDelegate,UITableViewDataSource {

    var dataTable:UITableView!
    var page = NSInteger()
    var rootArray = NSMutableArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "资料"

        self.page = 1;
        
        dataTable = UITableView(frame: CGRect(x: 0,y: self.view_startY, width: self.view.frame.size.width, height: self.view.frame.size.height),style:UITableViewStyle.grouped);
        dataTable.delegate = self;
        dataTable.dataSource = self;
        self.view.addSubview(dataTable);
        dataTable.sd_layout().spaceToSuperView(UIEdgeInsetsMake(0, 0, 0, 0))
        
        self.loadTableViewData()
        
  

}
    //默认返回一组
    func numberOfSections(in tableView: UITableView) -> Int
    {
        return 1;
    }
    
    //  返回行数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return self.rootArray.count;
    }
    
    //1.3 返回行高
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        let dic:NSDictionary = self.rootArray.object(at: indexPath.row) as! NSDictionary
        let strings  = dic.object(forKey: "content") as? String
        
//        let flot:CGFloat =  self.dataTable.cellHeightForIndexPath(indexPath, model: <#T##AnyObject!#>, keyPath: <#T##String!#>, cellClass: <#T##AnyClass!#>, contentViewWidth: <#T##CGFloat#>)
        
        return strings!.height(for: UIFont.systemFont(ofSize: 14), width: self.view.frame.size.width-40) + 20;
    }
    
//    //1.4每组的头部高度
//    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
//    {
//        return 10;
//    }
//    //1.5每组的底部高度
//    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
//    {
//        return 1;
//    }
    //1.6 返回数据源
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let identifier="identtifier";
        var cell=tableView.dequeueReusableCell(withIdentifier: identifier);
        if(cell == nil){
            cell=UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: identifier);
        }
        cell?.accessoryType = UITableViewCellAccessoryType.disclosureIndicator;
        
        let dic:NSDictionary = self.rootArray.object(at: indexPath.row) as! NSDictionary
        cell?.textLabel?.text = dic.object(forKey: "content") as? String
        cell?.textLabel?.numberOfLines = 0;
        cell?.textLabel?.font = UIFont.systemFont(ofSize: 14)
        
        cell?.detailTextLabel?.text = dic.object(forKey: "updatetime") as? String
        cell?.detailTextLabel?.textColor = UIColor.gray
        
        return cell!;
    }
    //1.7 表格点击事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        tableView.deselectRow(at: indexPath, animated: true);
    }

    func loadTableViewData()
    {
        let dic  = ["page":"1","key":"414953412a51f8fdcc0e0f2e5ac2fc03","pagesize":"15"]
        ZYNetWorking.shared().postRequest("http://japi.juhe.cn/joke/content/text.from", parameters: dic, success: { (jsonData) -> Void in
//            let json:NSDictionary = jsonData.object(forKey: "result")
            
            let jsonDiC:NSDictionary =  jsonData as! NSDictionary
            
            let json:NSDictionary = jsonDiC.object(forKey: "result") as! NSDictionary
            
            
            let array = json.object(forKey: "data")
            self.rootArray.addObjects(from: array as! [AnyObject])
            print("=================\(self.rootArray)")
            self.dataTable.reloadData()
            
            }) { (error) -> Void in
                
        }
        
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
